<?php

require_once 'Zend/Loader/Autoloader.php';

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**	 
	 * Front Controller
	 */
	private static $front = null;

	/**
	 * Application configuration	 
	 */
	private static $configuration = null;
    
    /**
     * Application configuration
     */
    private static $prefixCaching = null;
		
	/**
	 * Registration of my name space
	 */
    protected function initNamespace()
    {    	
    	Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true)
                                             ->suppressNotFoundWarnings(false);
    }	
    
    /**
     * Init application configuration
     */
    protected function initAppConfiguration()
    {
    	$appConfigPath = APPLICATION_PATH.'/configs/application-'.APP_ENV.'.ini';
        $configuration = new Zend_Config_Ini($appConfigPath);
        self::$configuration = $configuration->toArray();
        Zend_Registry::set(APP_CONFIG, self::$configuration);
       
    }
    
               
    /**
     * Routers setup
     */
    protected function initRouters()
    {
    	//Route Detail
    	//Route Cate
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'cate_route');
        $cate_route = new Zend_Controller_Router_Route_Regex('tin-tuc(?:/(?!vne*)(.+?)?)?',array('module' => 'default', 'controller' => 'cate', 'action' => 'index'), array(1 => "catename"), 'tin-tuc%s');

        //Route Detail
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'detail_route');
        $detail_route = new Zend_Controller_Router_Route_Regex('tin-tuc/(.+)/(.+)-(\d+)\.html',array('module' => 'default', 'controller' => 'detail', 'action' => 'index', 'page' => 1),array(1 => "catecode", 2 => "title", 3 => "id"),'%s/%s-%d.html');

        //Route Detail Page
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'detail_page_route');
        $detail_page_route = new Zend_Controller_Router_Route_Regex('tin-tuc/(.+)/(.+)-(\d+)-p(\d+)\.html$',array('module' => 'default', 'controller' => 'detail', 'action' => 'index'),array(1 => "catecode", 2 => "title", 3 => "id", 4 => "page"),'%s/%s-%d-%d.html');
        
        //Route Video Detail
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'video_detail_route');
        $video_route = new Zend_Controller_Router_Route_Regex('video/(.+)-(\d+)\.html$',array('module' => 'default', 'controller' => 'video', 'action' => 'index'),array(1 => "title", 2 => "videoid"),'%s-%d.html');
        
        // Route video ajax
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'video_ajax_route');
        $video_ajax_route = new Zend_Controller_Router_Route_Regex('video/ajax-([a-z\-]+)',array('module' => 'default', 'controller' => 'video', 'action' => 'index'), array(1 => 'action'), '%s');

        //Route Video Cate
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'video_cate_route');
        $video_cate_route = new Zend_Controller_Router_Route_Regex('video/((?!vne*)(.+?)?)?',array('module' => 'default', 'controller' => 'video', 'action' => 'index'), array(1=>"catename"));

        //Route Photo Detail
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'photo_detail_route');
        $photo_route = new Zend_Controller_Router_Route_Regex('photo/(.+)/(.+)-(\d+)-(\d+)\.html',array('module' => 'default', 'controller' => 'photo', 'action' => 'detail'),array(1 => "catecode", 2 => "title", 3 => "albumid", 4 => "page"),'%s/%s-%d-%d.html');

        //Route Photo Cate
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'photo_cate_route');
        $photo_cate_route = new Zend_Controller_Router_Route_Regex('photo/((?!vne*)(.+?)?)?',array('module' => 'default', 'controller' => 'photo', 'action' => 'index'), array(1=>"catename"));

        //Bang xep hang
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'ranking_route');
        $ranking_route = new Zend_Controller_Router_Route_Regex('bang-xep-hang/(.+)-(\d+)-(\d+)\.html',array('module' => 'default', 'controller' => 'ranking', 'action' => 'doibong'), array(1 => 'league', 2 => 'lid', 3 => 'sid'), '%s-%d-%d.html');

        //Lịch thi đấu
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'fixture_route');
        $fixture_route = new Zend_Controller_Router_Route('lich-thi-dau/:action/*',array('module' => 'default', 'controller' => 'fixture', 'action' => 'index'));

        //Tran dau
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'match_route');
        $match_route = new Zend_Controller_Router_Route_Regex('([a-z\-]+)/tran-([a-z0-9\-]+)-(\d+)',array('module' => 'default', 'controller' => 'match', 'action' => 'index'), array(1 => 'action', 2 => 'title', 3 => 'mid'), '%s-%s-%d.html');

        //Default module
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'default');
        $default_router = new Zend_Controller_Router_Route(':controller/:action/*',array('controller' => 'index', 'action' => 'index', 'module' => 'default'));

        //Route More News
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'newsmore_route');
        $newsmore_route = new Zend_Controller_Router_Route_Regex('xem-them/([a-z0-9\-]*)-(\d+)-(\d+)-(\d+)\.html',array('module' => 'default', 'controller' => 'newsmore', 'action' => 'index'),array(1 => "title", 2 => "id",3 => "fo",4 => "page"),'%s-%d-%d-%d.html');

        //Route Tag News
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'tagnews_route');
        $tagnews_route = new Zend_Controller_Router_Route_Regex('([a-z0-9\-]*)/tag-(\d+)-(\d+)\.html',array('module' => 'default', 'controller' => 'tagnews', 'action' => 'index'),array(1 => "tagname", 2 => "id",3 => "page"),'%s-%d-%d.html');

        //Route Topic News
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'topicnews_route');
        $topicnews_route = new Zend_Controller_Router_Route_Regex('([a-z0-9\-]*)/topic-(\d+)-(\d+)\.html',array('module' => 'default', 'controller' => 'topicnews', 'action' => 'index'),array(1 => "topicname", 2 => "id",3 => "page"),'%s-%d-%d.html');

        /**
         * Define router for CLB Toi Yeu
         */
        //Clbtoiyeu router
        self::$front->addControllerDirectory(APPLICATION_PATH.'/modules/clbtoiyeu/controllers', 'clbtoiyeu');
        $clbtoiyeu_router = new Zend_Controller_Router_Route('clbtoiyeu/:controller/:action/*', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'index'));

        //Detail clbty router
        self::$front->addControllerDirectory(APPLICATION_PATH.'/modules/clbtoiyeu/controllers', 'detail_clbty_route');
        $detail_clbty_route     = new Zend_Controller_Router_Route_Regex('clbtoiyeu/chi-tiet/(.+)-(\d+)\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'detail'), array(1=>"title", 2=>"id"), 'chi-tiet/%s-%d.html');

        //Rule router
        self::$front->addControllerDirectory('/modules/clbtoiyeu/controllers', 'rule_clbty_route');
        $rule_clbty_route       = new Zend_Controller_Router_Route_Regex('clbtoiyeu/the-le\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'rule'), null, 'the-le.html');

        //Award router
        self::$front->addControllerDirectory('/modules/clbtoiyeu/controllers', 'award_clbty_route');
        $award_clbty_route      = new Zend_Controller_Router_Route_Regex('clbtoiyeu/giai-thuong\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'award'), null, 'giai-thuong.html');

        //Error router
        self::$front->addControllerDirectory('/modules/clbtoiyeu/controllers', 'error_clbty_route');
        $error_clbty_route      = new Zend_Controller_Router_Route_Regex('clbtoiyeu/error\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'error'), null, 'error.html');

        //My post router
        self::$front->addControllerDirectory('/modules/clbtoiyeu/controllers', 'mypost_clbty_route');
        $mypost_clbty_route     = new Zend_Controller_Router_Route_Regex('clbtoiyeu/bai-thi-cua-ban-(\d+)\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'mypost'), array(1=>'page'), 'bai-thi-cua-ban-%d.html');

        //List router
        self::$front->addControllerDirectory('/modules/clbtoiyeu/controllers', 'list_clbty_route');
        $list_clbty_route       = new Zend_Controller_Router_Route_Regex('clbtoiyeu/danh-sach/(.+)-(\d+)-(\d+)\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'list'), array(1=>'catecode', 2=>'type', 3=>'page'), 'danh-sach/%s-%d-%d.html');

        //Post router
        self::$front->addControllerDirectory('/modules/clbtoiyeu/controllers', 'post_clbty_route');
        $post_clbty_route       = new Zend_Controller_Router_Route_Regex('clbtoiyeu/post\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'post'), null, 'post.html');

        //Preview router
        self::$front->addControllerDirectory('/module/clbtoiyeu/controllers', 'preview_clbty_route');
        $preview_clbty_route    = new Zend_Controller_Router_Route_Regex('clbtoiyeu/preview\.html', array('module'=>'clbtoiyeu', 'controller'=>'index', 'action'=>'preview'), null, 'preview.html');

        
        //Tran cau dinh
        self::$front->addControllerDirectory(APPLICATION_PATH . '/modules/default/controllers', 'matchextend_route');
        $matchextend_route = new Zend_Controller_Router_Route_Regex('tran-cau-dinh/(.+)-(.+)-(\d+)\.html',array('module' => 'default', 'controller' => 'matchextend', 'action' => 'index'), array(1 => 'team1', 2 => 'team2', 3 => 'mid'), '%s-%s-%d.html');
        
        
        //Add router
        $routers =  self::$front->getRouter();
        $routers->addRoute('default_router', $default_router);
        $routers->addRoute('video_cate_route', $video_cate_route);
        $routers->addRoute('video_route', $video_route);
        $routers->addRoute('video_ajax_route', $video_ajax_route);
        $routers->addRoute('cate_route', $cate_route);
        $routers->addRoute('detail_route', $detail_route);
        $routers->addRoute('detail_page_route', $detail_page_route);
        $routers->addRoute('photo_cate_route', $photo_cate_route);
        $routers->addRoute('photo_route', $photo_route);
        $routers->addRoute('newsmore_route', $newsmore_route);
        $routers->addRoute('ranking_route', $ranking_route);
        $routers->addRoute('fixture_route', $fixture_route);
        $routers->addRoute('tagnews_route', $tagnews_route);
        $routers->addRoute('topicnews_route', $topicnews_route);
        $routers->addRoute('match_route', $match_route);
        /**
         * Add router for clbtoiyeu
         */
        $routers->addRoute('clbtoiyeu_router', $clbtoiyeu_router);
        $routers->addRoute('detail_clbty_route', $detail_clbty_route);
        $routers->addRoute('rule_clbty_route', $rule_clbty_route);
        $routers->addRoute('award_clbty_route', $award_clbty_route);
        $routers->addRoute('error_clbty_route', $error_clbty_route);
        $routers->addRoute('mypost_clbty_route', $mypost_clbty_route);
        $routers->addRoute('list_clbty_route', $list_clbty_route);
        $routers->addRoute('post_clbty_route', $post_clbty_route);
        $routers->addRoute('preview_clbty_route', $preview_clbty_route);
        //Route trancaudinh
        $routers->addRoute('matchextend_route', $matchextend_route);
        //Set new router
        self::$front->setRouter($routers);

        //Cleanup
        unset($cate_route, $detail_route, $photo_route, $default_router, $search_route, $clbtoiyeu_router, $detail_clbty_route, $rule_clbty_route, $award_clbty_route, $error_clbty_route, $mypost_clbty_route, $list_clbty_route, $post_clbty_route, $preview_clbty_route);
    }
    
    /**
     * Helper controller setup
     */
    protected function initControllerHelper()
    {
    	Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/modules/default/controllers/helpers');       
    }
    
    /**
     * Zend_Front_Controller created on each request
     */
    protected function initFrontController()
    {
    	//Init frontController
    	self::$front = Zend_Controller_Front::getInstance();
    	
    	//Set the current environment
    	self::$front->setParam(ENVIRONMENT, APP_ENV);

        //Set default if error controller
        self::$front->setParam('useDefaultControllerAlways', true);
    	    	
        //Enable error controller plugin
        if(APP_ENV == 'development')
        {
            self::$front->throwExceptions(true);	
        }
        else
        {
        	self::$front->throwExceptions(false);
        }
        
        //Set new router
        $this->initRouters();
        
        //Controller helpers
        $this->initControllerHelper();        
    }
    
    /**
     * Plugin Front Init
     */
    protected function initFrontPlugin()
    {
    	//Add Env Plugin
        self::$front->registerPlugin(new Fpt_Plugin_Env());
    }

    /**
    * Session setup
    */
    protected function initSession()
    {
         /* Set session cookie */
        ini_set('session.cookie_domain', SESSION_NAMESPACE);
        $session = new Zend_Session_Namespace(SESSION_NAMESPACE);
       
        //Check time load spam
        if(!isset($_SESSION['loadTime']))
        {
            $_SESSION['loadTime'] = time();

        }
        
        //Check is first post
        if(!isset($_SESSION['isFirstLoad']))
        {
            $_SESSION['isFirstLoad'] = 1;
        }

        /* Set to registry */
        Zend_Registry::set(GLOBAL_SESSION_CONFIG, $session);
    }

    /**
     * Run the application
     *
     * Checks to see that we have a default controller directory. If not, an
     * exception is thrown.
     *
     * If so, it registers the bootstrap with the 'bootstrap' parameter of
     * the front controller, and dispatches the front controller.
     *
     * @return void
     * @throws Zend_Exception
     */
    public function run()
    {   
    	//Init namspace
    	$this->initNamespace();

        //Init session
        $this->initSession();

    	//Init Application Configuration
    	$this->initAppConfiguration();
    	
    	//Init Front Controller
    	$this->initFrontController();

        
        //Check default module       
        if(null === self::$front->getControllerDirectory(self::$front->getDefaultModule()))
        {
            throw new Zend_Exception(
                'No default controller directory registered with front controller'
            );
        }
      
        //Set controller plugin
        $this->initFrontPlugin();
              
        //Dispatch controller
        self::$front->setParam('bootstrap', $this);
        self::$front->dispatch();
        exit(1);
    }
}

